{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Second Largest Digit in a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: secondHighest"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串中第二大的数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个混合字符串 <code>s</code> ，请你返回 <code>s</code> 中 <strong>第二大 </strong>的数字，如果不存在第二大的数字，请你返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p><strong>混合字符串 </strong>由小写英文字母和数字组成。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"dfa12321afd\"\n",
    "<b>输出：</b>2\n",
    "<b>解释：</b>出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"abc1111\"\n",
    "<b>输出：</b>-1\n",
    "<b>解释：</b>出现在 s 中的数字只包含 [1] 。没有第二大的数字。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= s.length <= 500</code></li>\n",
    "\t<li><code>s</code> 只包含小写英文字母和（或）数字。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [second-largest-digit-in-a-string](https://leetcode.cn/problems/second-largest-digit-in-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [second-largest-digit-in-a-string](https://leetcode.cn/problems/second-largest-digit-in-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"dfa12321afd\"', '\"abc1111\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        result = -1\n",
    "        s_set = set()\n",
    "        for i in range(len(s)):\n",
    "            c = ord(s[i])\n",
    "            if 48 <= c <= 57:\n",
    "                s_set.add(int(s[i]))\n",
    "        nums_list = list(s_set)\n",
    "        if len(nums_list) > 1:\n",
    "            nums_list.sort()\n",
    "            result = nums_list[-2]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(9,-1,-1):\n",
    "            if str(i) in s:\n",
    "                if count == 1:\n",
    "                \treturn i\n",
    "                count += 1\n",
    "        return -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = second = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():   #如果第s[i]为字母则跳过,为数字才往下进行\n",
    "                num = int(c)   #如果第s[i]为数字，则令num 表示s[i]对应的十进制数字\n",
    "                if num>first:\n",
    "                    second = first\n",
    "                    first = num\n",
    "                elif second<num<first:\n",
    "                     second = num\n",
    "        return second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def secondHighest(self, s: str) -> int:\r\n",
    "        ans = list()\r\n",
    "        for i in s:\r\n",
    "            try:\r\n",
    "                ans.append(int(i))\r\n",
    "            except ValueError:\r\n",
    "                continue\r\n",
    "        if len(set(ans)) < 2:\r\n",
    "            return -1\r\n",
    "        else:\r\n",
    "            return (sorted(set(ans),reverse=True)[1])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        ls = set()\n",
    "        for i in s:\n",
    "            if i in '0123456789':\n",
    "                ls.add(int(i))\n",
    "        if len(ls) <= 1:\n",
    "            return -1\n",
    "        else:\n",
    "            return sorted(list(ls))[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        ls = set()\n",
    "        for i in s:\n",
    "            if i in '0123456789':\n",
    "                ls.add(int(i))\n",
    "        if len(ls) <= 1:\n",
    "            return -1\n",
    "        else:\n",
    "            return sorted(list(ls))[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        num = list(map(int,[i for i in s if i.isdigit()]))\n",
    "        if len(set(num)) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = num[0]\n",
    "        for i in num:\n",
    "            if i < max_num:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s_info = set(list(s))\n",
    "        tmp = list()\n",
    "        for val in s_info:\n",
    "            if '0' <= val <= '9':\n",
    "                tmp.append(val)\n",
    "        if len(tmp) < 2:\n",
    "            return -1\n",
    "        tmp.sort()\n",
    "        return int(tmp[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "\n",
    "        largest = -1\n",
    "        second_largest = -1\n",
    "\n",
    "        for char in s:\n",
    "            if char.isdigit():\n",
    "                num = int(char)\n",
    "                if num > largest:\n",
    "                    second_largest = largest\n",
    "                    largest = num\n",
    "                elif num < largest and num > second_largest:\n",
    "                    second_largest = num\n",
    "\n",
    "        return second_largest\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def secondHighest(self, s: str) -> int:\n",
    "    a = b = -1\n",
    "    for c in s:\n",
    "      if c.isdigit():\n",
    "        t = int(c)\n",
    "        if t > a:\n",
    "          b = a\n",
    "          a = t\n",
    "        elif b < t < a:\n",
    "          b = t\n",
    "    return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(set(num)) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s = set(s)\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(num) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num[1:]:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = second = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():   #如果第s[i]为字母则跳过,为数字才往下进行\n",
    "                num = int(c)\n",
    "                if num>first:\n",
    "                    second = first\n",
    "                    first = num\n",
    "                elif second<num<first:\n",
    "                     second = num\n",
    "        return second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s = set(s)\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(num) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num[1:]:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        res = list(set(re.findall('\\d',s)))\n",
    "        if len(res)<=1:\n",
    "            return -1\n",
    "        return int(sorted(res,reverse=True)[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        all_int = \"1234567890\"\n",
    "        result = []\n",
    "        for i in s:\n",
    "            if i in all_int and int(i) not in result:\n",
    "                result.append(int(i))\n",
    "        sort_result = sorted(result, reverse=True)\n",
    "        if len(result) >= 2:\n",
    "            return sort_result[1]\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        # nums= []\n",
    "        # for i in s:\n",
    "        #     if i.isdigit():\n",
    "        #         nums.append(int(i))\n",
    "        \n",
    "        # sNums = set(nums)\n",
    "        # s = list(sNums)\n",
    "\n",
    "        # if len(s) >= 2:\n",
    "        #     s.sort()\n",
    "        #     return  s[-2]\n",
    "        \n",
    "        # else:\n",
    "        #     return -1\n",
    "\n",
    "        first = sec = -1\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                if int(i)>first:\n",
    "                    sec = first\n",
    "                    first = int(i)\n",
    "                elif int(i) > sec and int(i) < first:\n",
    "                    sec = int(i)\n",
    "                \n",
    "        return sec\n",
    "                   \n",
    "           \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a, b = -1, -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                if int(c) > a:\n",
    "                    a, b = int(c), a\n",
    "                elif a > int(c) > b:\n",
    "                    b = int(c)\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        ss = []\n",
    "        for ch in s:\n",
    "            if ch.isdigit():\n",
    "                ss.append(int(ch))\n",
    "        if len(set(ss))<=1:return -1\n",
    "        ss1 = sorted(set(ss))\n",
    "        return ss1[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(set(num)) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s = set(s)\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(num) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a = b = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                v = int(c)\n",
    "                if v > a:\n",
    "                    a, b = v, a\n",
    "                elif b < v < a:\n",
    "                    b = v\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a = b = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                v = int(c)\n",
    "                if v > a:\n",
    "                    a, b = v, a\n",
    "                elif b < v < a:\n",
    "                    b = v\n",
    "        return b\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        result_list = []\n",
    "        re_result_list=[]\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                result_list.append(int(i))\n",
    "        for i in result_list:\n",
    "            if i not in re_result_list:\n",
    "                re_result_list.append(i)\n",
    "        if len(re_result_list) ==1 or len(re_result_list)==0:\n",
    "            return -1\n",
    "        else:\n",
    "            max_number = max(re_result_list)\n",
    "            for i in re_result_list:\n",
    "                if i == max_number:\n",
    "                    re_result_list.remove(i)\n",
    "            return max(re_result_list)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        num = list(map(int,[i for i in s if i.isdigit()]))\n",
    "        if len(set(num)) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = num[0]\n",
    "        for i in num:\n",
    "            if i < max_num:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first=second=-1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                num = int(c)\n",
    "                if num>first:\n",
    "                    second=first\n",
    "                    first=num\n",
    "                elif second<num<first:\n",
    "                    second=num\n",
    "        return second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        lst = [0] * 10\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                lst[int(c)] = 1\n",
    "        ans = [i for i, v in enumerate(lst) if v == 1]\n",
    "        return ans[-2] if len(ans) > 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        cnt = [0 for _ in range(10)]\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            c = s[i]\n",
    "            if c >= '0' and c <= '9':\n",
    "                cnt[int(c)] += 1\n",
    "        \n",
    "        first = False\n",
    "\n",
    "        for i in range(9, -1, -1):\n",
    "            if cnt[i] != 0:\n",
    "                if not first:\n",
    "                    first = True\n",
    "                else:\n",
    "                    return i\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a = b = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                v = int(c)\n",
    "                if v > a:\n",
    "                    a, b = v, a\n",
    "                elif b < v < a:\n",
    "                    b = v\n",
    "        return b\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s = set(s)\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(num) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num[1:]:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = second = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                num = int(c)\n",
    "                if num > first:\n",
    "                    second = first\n",
    "                    first = num\n",
    "                elif second < num < first:\n",
    "                    second = num\n",
    "        return second\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        nums = [int(item) for item in s if item.isdigit()]\n",
    "        nums = list(set(nums))\n",
    "        nums.sort()\n",
    "\n",
    "        return nums[-2] if len(nums)>1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        table = [0] * 10\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                table[int(c)] = 1\n",
    "        flag = False\n",
    "        for i in range(9, -1, -1):\n",
    "            if table[i]:\n",
    "                if flag:\n",
    "                    return i\n",
    "                else:\n",
    "                    flag = True\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        list = []\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                if int(i) not in list:\n",
    "                    list.append(int(i))\n",
    "        list.sort()\n",
    "        if len(list)==1 or len(list)==0:\n",
    "            return -1\n",
    "        elif len(list)==2:\n",
    "            return list[0]\n",
    "        else:\n",
    "            return list[-2]\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = -1\n",
    "        second = -1      \n",
    "        for i in s:\n",
    "            if ord(i)>=ord(\"0\") and ord(i)<=ord(\"9\"):\n",
    "                if int(i)>first:\n",
    "                    second = first\n",
    "                    first = int(i)\n",
    "                elif int(i)>second and int(i) != first:\n",
    "                    second = int(i)\n",
    "\n",
    "        return second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = second = -1\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                num = int(i)\n",
    "                if num > first:\n",
    "                    second = first\n",
    "                    first = num\n",
    "                elif second < num < first:\n",
    "                    second = num\n",
    "        return second\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        list = []\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                if int(i) not in list:\n",
    "                    list.append(int(i))\n",
    "        list.sort()\n",
    "        if len(list)==1 or len(list)==0:\n",
    "            return -1\n",
    "        elif len(list)==2:\n",
    "            return list[0]\n",
    "        else:\n",
    "            return list[-2]\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        ans=set()\n",
    "        for i in s:\n",
    "            if \"0\"<=i<=\"9\":\n",
    "                ans.add(int(i))\n",
    "        return sorted(list(ans),reverse=True)[1] if len(ans)>1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        v = set()\n",
    "        for x in s:\n",
    "            if '0' <= x <= '9':\n",
    "                v.add(int(x))\n",
    "        v = sorted(list(v), reverse=True)\n",
    "        if len(v) <= 1:\n",
    "            return -1\n",
    "        return v[1]\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        l=[]\n",
    "        for i in s:\n",
    "            if i.isdigit()==True:\n",
    "               l.append(int(i))\n",
    "        l=sorted(list(set(l)))\n",
    "        if len(l)<2:\n",
    "                 return -1\n",
    "        else:\n",
    "                 return l[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        res=[]\n",
    "        size=len(s)\n",
    "        for i in range(size):\n",
    "            if s[i].isdigit():\n",
    "                if int(s[i]) not in res:\n",
    "                    res+=[int(s[i])]\n",
    "        if len(res)<=1:\n",
    "            return -1\n",
    "        else:\n",
    "            res.sort()\n",
    "            return res[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s = set(s)\n",
    "        num = [i for i in s if i.isdigit()]\n",
    "        if len(num) < 2:\n",
    "            return -1\n",
    "        num.sort(reverse=True)\n",
    "        max_num = int(num[0])\n",
    "        for i in num:\n",
    "            if int(i) < max_num:\n",
    "                return int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(9,-1,-1):\n",
    "            if str(i) in s:\n",
    "                if count == 1:\n",
    "                \treturn i\n",
    "                count += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = second = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                num = int(c)\n",
    "                if num > first:\n",
    "                    second = first\n",
    "                    first = num\n",
    "                elif second < num < first:\n",
    "                    second = num\n",
    "        return second\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        ad={}\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                ad[int(i)]=ad.get(int(i),0)+1\n",
    "        r=[k for k in ad]\n",
    "        r.sort()\n",
    "        if len(r)>=2:\n",
    "            return r[-2]\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a={}\n",
    "        for i in s:\n",
    "            if i.isnumeric():\n",
    "                if i in a:\n",
    "                    a[i]+=1\n",
    "                else:\n",
    "                    a[i]=1\n",
    "        a=list(a.keys())\n",
    "        if len(a)<2:\n",
    "            return -1\n",
    "        a.sort()\n",
    "        return int(a[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        l = sorted(list(set([int(i) for i in s if i.isdigit()])))\n",
    "        if len(l) >= 2:\n",
    "            return l[-2]\n",
    "        else:\n",
    "            return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a = b = -1\n",
    "        for c in s:\n",
    "            if c.isdigit():\n",
    "                v = int(c)\n",
    "                if v > a:\n",
    "                    a, b = v, a\n",
    "                elif b < v < a:\n",
    "                    b = v\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        s = list(filter(lambda x: ord(x) <= 57, list(s)))\n",
    "        s.sort(reverse = True)\n",
    "        if s:\n",
    "            x = s[0]\n",
    "        for i in s:\n",
    "            if ord(i) < ord(x):\n",
    "                return int(i)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        ns = sorted(list(set([int(c) for c in s if c.isdigit()])), reverse=True)\n",
    "        return ns[1] if len(ns) > 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        nums = sorted(set([int(i) for i in s if ord(i)>=ord(\"0\") and ord(i)<=ord(\"9\")]))\n",
    "        return nums[-2] if len(nums)>1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        tmp = []\n",
    "        for i in s:\n",
    "            if '0'<=i<='9':\n",
    "                if int(i) in tmp:\n",
    "                    continue\n",
    "                else:\n",
    "                    tmp.append(int(i))\n",
    "        if len(tmp)==1 or len(tmp)==0:\n",
    "            return -1\n",
    "        else:\n",
    "            tmp = sorted(tmp,reverse=True)\n",
    "            return tmp[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        res = sorted(list(set([int(s[i]) for i in range(len(s)) if s[i].isdigit()])))\n",
    "        return -1 if len(res) < 2 else res[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a = b = min(s)\n",
    "        if a.isalpha():\n",
    "            return -1\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                if i > a:\n",
    "                    a, b = i, a\n",
    "                elif i < a and i > b:\n",
    "                    b = i\n",
    "        return int(b) if a > b else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        res = []\n",
    "        for i in s:\n",
    "            if i>='a'and i<=\"z\":\n",
    "                continue\n",
    "            else:\n",
    "                res.append(int(i))\n",
    "        res = set(res)\n",
    "        if len(res)<=1:\n",
    "            return -1\n",
    "        else:\n",
    "            res.remove(max(res))\n",
    "            return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        list1 = list(s)\n",
    "        list2 = []\n",
    "        for i in list1:\n",
    "            if i in ascii_lowercase:\n",
    "                continue\n",
    "            else:\n",
    "                list2.append(int(i))\n",
    "        if len(list2) <= 1:\n",
    "            return -1\n",
    "        set1 = sorted(set(tuple(list2)), reverse=True)\n",
    "        if len(set1) == 1:\n",
    "            return -1\n",
    "        return set1[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        t=set()\n",
    "        for i in s:\n",
    "            if '0'<=i<='9':\n",
    "                t.add(int(i))\n",
    "        if len(t)>=2:\n",
    "            return sorted(list(t))[-2]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        output = re.sub(r'[a-zA-Z]', '', s)\n",
    "        res = []\n",
    "        for i in output:\n",
    "            res.append(i)\n",
    "\n",
    "        res=list(set(res))\n",
    "        res.sort(reverse=True)\n",
    "        if len(res) <=1:\n",
    "            return -1\n",
    "\n",
    "        else:\n",
    "            return int(res[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a = b = min(s)\n",
    "        if a.isalpha():\n",
    "            return -1\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                if i > a:\n",
    "                    a, b = i, a\n",
    "                elif i < a and i > b:\n",
    "                    b = i\n",
    "        return int(b) if a > b else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "\n",
    "\n",
    "        pattern = r\"\\d\"\n",
    "\n",
    "        data_list = re.findall(pattern,s)\n",
    "\n",
    "        data_list.sort(reverse=True)\n",
    "\n",
    "        print(data_list)\n",
    "\n",
    "        diff = 1\n",
    "\n",
    "        for i in range(1,len(data_list)):\n",
    "\n",
    "            if data_list[i] != data_list[i-1]:\n",
    "\n",
    "                diff += 1\n",
    "\n",
    "                if diff == 2:\n",
    "\n",
    "                    return int(data_list[i])\n",
    "        \n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        num_lst = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isdigit():\n",
    "                num_lst.append(int(s[i]))\n",
    "        if num_lst == []:\n",
    "            return -1\n",
    "        max_num = max(num_lst)\n",
    "        size = len(num_lst)\n",
    "        for i in reversed(range(size)):\n",
    "            if num_lst[i] == max_num:\n",
    "                num_lst.pop(i)\n",
    "        #print(num_lst)\n",
    "        if num_lst != []:\n",
    "            return max(num_lst)\n",
    "        else:\n",
    "            return -1\n",
    "        #print(num_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        x=[int(num) for num in s if '0'<=num<='9' ]\n",
    "        x=list(set(x))\n",
    "        x=sorted(x)\n",
    "        if len(x)>=2:\n",
    "            return x[-2]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        nums=[]\n",
    "        for c in s:\n",
    "            if not c.isalpha():\n",
    "                nums.append(c)\n",
    "        print(nums)\n",
    "        nums=list(set(nums))\n",
    "        st=sorted(nums,reverse=True)\n",
    "        print(st)\n",
    "        if len(st)<2:\n",
    "            return -1\n",
    "        else:\n",
    "            return int(st[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        a=set()\n",
    "        for i in s:\n",
    "            if i in '0123456789':\n",
    "                a.add(i)\n",
    "        if len(a) <2:\n",
    "            return -1\n",
    "        a=list(a)\n",
    "        a.sort(reverse=True)\n",
    "        return int(a[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        int_set = set()\n",
    "        for ch in s:\n",
    "            if(ch.isdigit() == True):\n",
    "                int_set.add(eval(ch))\n",
    "        if(len(int_set) < 2):\n",
    "            return -1\n",
    "        int_list = list(int_set)\n",
    "        int_list.sort()\n",
    "        return int_list[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        first = -1\n",
    "        second = -1\n",
    "        for n in s:\n",
    "            if n < '0' or n > '9':\n",
    "                continue\n",
    "            m = int(n)\n",
    "            if m > first:\n",
    "                second = first\n",
    "                first = m\n",
    "            elif m < first and m > second:\n",
    "                second = m\n",
    "        return second\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def secondHighest(self, s: str) -> int:\n",
    "        set1=set()\n",
    "        s=[i for i in s]\n",
    "        for i in s:\n",
    "            if i.isdigit():\n",
    "                set1.add(i)\n",
    "        if len(set1)<2:\n",
    "            return -1\n",
    "        else:\n",
    "            aa=sorted([int(i) for i in set1],reverse=True)\n",
    "            return aa[1]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
